package com.ucode.application.security;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ucode.springboot.starter.security.entity.UcodeGrantedAuthority;
import com.ucode.springboot.starter.security.service.UcodeGrantedAuthorityService;
import com.ucode.sys.model.RoleMenu;
import com.ucode.sys.service.RoleService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;

@Service
public class UcodeGrantedAuthorityServiceImpl implements UcodeGrantedAuthorityService {

    @Autowired
    public RoleService roleService;
    
    @Override
    public List<UcodeGrantedAuthority> getGrantedAuthoritys(String userId) throws RuntimeException {
        Long _userId = Long.valueOf(userId);
        List<RoleMenu> permissions = roleService.findUserPermissions(_userId);
        List<UcodeGrantedAuthority> list = null;
        if(CollUtil.isNotEmpty(permissions)){
            HashSet<String> set = new HashSet<String>();
            list = new ArrayList<UcodeGrantedAuthority>();
            for(RoleMenu roleMenu : permissions){
                if(StrUtil.isNotBlank(roleMenu.getPermission()) && set.add(roleMenu.getPermission())){
                    list.add(new UcodeGrantedAuthority(roleMenu.getPermission()));
                }
            }
        }
      return list;
    }

}
